package com.zyuan.lawyerport.operation;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 网络操作类
 * Created by JustTong on 2018/12/4.
 */
public class NetworkCore {
    private static Api api;
    private static final String BASE_URL = "http://cyc.doing365.com/";

    public static Api getInstances() {
        if (api == null) {
            synchronized (NetworkCore.class) {
                if (api == null) {
                    //日志插入器
                    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
                    //设置查看日志的等级
                    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            .addInterceptor(httpLoggingInterceptor)
                            .connectTimeout(10, TimeUnit.SECONDS)
                            .readTimeout(10, TimeUnit.SECONDS)
                            .writeTimeout(10, TimeUnit.SECONDS)
                            .build();
                    Retrofit retrofit = new Retrofit.Builder()
                            //设置数据解析器
                            .addConverterFactory(GsonConverterFactory.create())
                            //设置网络请求的Url地址
                            .baseUrl(BASE_URL)
                            .client(okHttpClient)
                            .build();
                    api = retrofit.create(Api.class);
                }
            }
        }
        return api;
    }
}
